#include "config.h"
#include "pcb.h"
#include "schedule.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct PCB *init_processes(int proc_count) {
  if (proc_count <= 0) {
    return NULL;
  }

  srand(time(NULL));

  struct PCB *res, *p;
  res = p = alloc_PCB(1);
  for (int i = 2; i <= proc_count; ++i) {
    p->next = alloc_PCB(i);
    p = p->next;
  }
  return res;
}

void free_processes(struct PCB *processes) {
  struct PCB *p;
  while (processes) {
    p = processes;
    processes = processes->next;
    free(p);
  }
}

int main() {
  if (read_config() == -1) {
    perror("ERROR: failed to read config");
    return -1;
  }

  struct PCB *processes = init_processes(proc_count);
  int choice;
  printf("TYPE THE ALGORITHM (1-PRIORITY, 2-ROUND ROBIN): ");
  scanf("%d", &choice);
  getchar();
  switch (choice) {
  case 1:
    priority_schedule(&processes);
    break;
  case 2:
    round_robin_schedule(&processes);
    break;
  default:
    printf("INVALID CHOICE\n");
  }
  free_processes(processes);

  return 0;
}